<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      //对象的深拷贝
      function deepClon(obj = {}) {
        //如果传入值不是对象或数组 或者为null直接返回
        if (typeof obj !== "object" || obj === null) {
          // throw new Error("请传入数组或者对象");
          return obj;
        }
        let result;
        // 判断是否是数组
        if (Array.isArray(obj)) {
          result = [];
        } else {
          result = {};
        }

        for (const [key, value] of Object.entries(obj)) {
          result[key] =
            typeof obj[key] === "object" ? deepClon(obj[key]) : obj[key];
        }
        return result;
      }
      let obj2 = {
        a: 1,
        b: 2,
        c: {
          d: {
            e: [1, 2, 3],
          },
        },
      };
      obj2.__proto__.niubi = "a";
      let obj1 = deepClon(obj2);
      obj1.c.d.e = 2;
      console.log(obj2, obj1);
      // function xiaoming(ing) {
      //   this.xiaoming = function () {
      //     console.log(ing);
      //   };
      //   // return this;
      // }
      // let a = new xiaoming("睡觉");

      // a.xiaoming("打飞机");
    </script>
  </body>
</html>
